$(function() {
	//将数据展示到页面上
	var flag = true;
	function Flag(a) {
		return flag = a;
	}
	// 进入页面加载系统消息列表的数据 
	loadYouLike();
	function loadYouLike() {
		var page = 0; // 页码数
		// dropload函数接口设置
		$('.indiana_records_content').dropload({
			scrollArea: window,
			// 上拉加载更多
			domDown: {
				domClass: 'dropload-down',
				// 滑动到底部显示内容
				domRefresh: '<div class="dropload-refresh">上拉加载更多</div>',
				// 内容加载过程中显示内容
				domLoad: '<div class="dropload-load"><span class="loading"></span>加载中...</div>',
				// 没有更多内容显示提示
				domNoData: '<div class="dropload-noData">我也是有深度的...</div>'
			},
			// 上拉加载更多 回调函数
			loadDownFn: function(me) {
				page++; // 每次请求，页码加1 
				if(flag) {
					var load1 = $.ajax({
						type: 'POST',
						url: 'http://a.91jfk.com/Product/Order/goods_index?p=' + page,
						dataType: 'json',
						success: function(res) {
							console.log(res);
							if(res.nowpage < res.totalpage) {
								Flag(true);
								if(res.code == '1') {
									for(var i = 0; i < res.msg.length; i++) {
										if(res.msg[i].state == "2") {
											var periods = get_Date(res.msg[i].periods);
											showItem(res.msg[i].thumbnail, res.msg[i].pname, periods, res.msg[i].num);
										} else if(res.msg[i].state == "4" && res.msg[i].user_type == 1) {
											var periods = get_Date(res.msg[i].periods);
											showItem1(res.msg[i].thumbnail, res.msg[i].pname, periods,res.msg[i].num,res.msg[i].luck_user, res.msg[i].order_num,res.msg[i].time);
										}else if(res.msg[i].state == "4" && res.msg[i].user_type == 2){
											var periods = get_Date(res.msg[i].periods);
											showItem2(res.msg[i].thumbnail, res.msg[i].pname, periods,res.msg[i].num,res.msg[i].luck_user, res.msg[i].order_num,res.msg[i].time);
										}
									}
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								} else {
									me.noData();
									me.lock();
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								}
							} else {
								if(res.code == '1') {
									for(var i = 0; i < res.msg.length; i++) {
										if(res.msg[i].state == "2") {
											var periods = get_Date(res.msg[i].periods);
											showItem( res.msg[i].thumbnail, res.msg[i].pname, periods, res.msg[i].num);
										} else if(res.msg[i].state == "4" && res.msg[i].user_type == 1) {
											var periods = get_Date(res.msg[i].periods);
											showItem1(res.msg[i].thumbnail, res.msg[i].pname, periods,res.msg[i].num,res.msg[i].luck_user, res.msg[i].order_num,res.msg[i].time);
										}else if(res.msg[i].state == "4" && res.msg[i].user_type == 2){
											var periods = get_Date(res.msg[i].periods);
											showItem2(res.msg[i].thumbnail, res.msg[i].pname, periods,res.msg[i].num,res.msg[i].luck_user, res.msg[i].order_num,res.msg[i].time);
										}

									}
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								} else {
									me.noData();
									me.lock();
									// 重置
									setTimeout(function() {
										me.resetload();
									}, 1500);
								}
								load1.abort();
								Flag(false);
								me.noData();
								me.lock();
								// 重置
								setTimeout(function() {
									me.resetload();
								}, 1500);
							}
						},
						// 加载出错
						error: function(err) {
							console.log('Ajax error!');
							// 即使加载出错，也得重置
							setTimeout(function() {
								me.resetload();
							}, 1500);
						}
					});
				} else {
					// 再往下已经没有数据，锁定
					// 显示无数据
					me.noData();
					me.lock();
					// 重置
					setTimeout(function() {
						me.resetload();
					}, 1500);
				}
			}
		})
	}
	//进行中商品列表
	function showItem(src, name, periods, num) {
		var list = $('<div class="list"><div class="shop"><img  src="' + src + '"/></div><div class="info"><p class="name">' + name + '</p><p>商品期数：<span>' + periods + '</span></p><p>本次下注：<span>' + num + '</span>注</p><p id="add">追加</p></div></div>');
		$(".box").append(list);
	}
	//已揭晓商品列表
	function showItem1(src, pname, periods, num, name, personNum,time) {
		var list = $('<div class="list"><div class="shop"><img  src="' + src + '"/></div><div class="info"><p class="name">' + pname + '</p><p>商品期数：<span>' + periods + '</span></p><p>本次下注：<span>' + num + '</span>注</p></div><img src="/Public/Product/img/duobaochenggong.png" class="chenggong"/><div class="check_num"><p>获奖者：<span>' + name + '</span></p><p><span>本次参与：<span>' + personNum + '</span>人次</span><span id="jiexiao">揭晓时间：<span>' + time + '</span></span></p></div></div>');
		$(".box").append(list);
	}
	function showItem2(src, pname, periods, num, name, personNum,time) {
		var list = $('<div class="list"><div class="shop"><img  src="' + src + '"/></div><div class="info"><p class="name">' + pname + '</p><p>商品期数：<span>' + periods + '</span></p><p>本次下注：<span>' + num + '</span>注</p></div><img src="/Public/Product/img/duobaoshibai.png" class="shibai"/><div class="check_num"><p>获奖者：<span>' + name + '</span></p><p><span>本次参与：<span>' + personNum + '</span>人次</span><span id="jiexiao">揭晓时间：<span>' + time + '</span></span></p></div></div>');
		$(".box").append(list);
	}

	$('.type:nth-child(2)').click(function() {
		window.location.href = "page_jinxingzhong.html";
	});
	$('.type:nth-child(3)').click(function() {
		window.location.href = "page_yijiexiao.html";
	});

	$(".indiana_records_content").on("click", "#add", function() {
		var num = $(this).prev().find('span').text() - 0;
		num++;
		$(this).prev().find('span').text(num);
	});
	//将时间戳转变为时间
	function get_Date(tm) {
		var d = new Date(tm * 1000); //根据时间戳生成的时间对象
		var date = (d.getFullYear()) + "" +
			(d.getMonth() + 1) + "" +
			(d.getDate());
		return date;
	}

})